![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Bug Proofing Visual Basic: A Guide to Error Handling and Prevention
To protect the program from this sort of bug, add a Case Else clause to the Select statement. If the Select statement reaches the Else code, the program should stop. Then if you define a new value and forget to add it to the Select statements cases, the program will stop and make the mistake obvious instead of continuing as if nothing was wrong.
Select Case Filter
Case filter_PREWITT Build a Prewitt filter.
:
Case filter_HIGH_PASS Build a high pass filter.
:
Case filter_LOW_PASS Build a low pass filter.
:
Case Else This should not happen.
Stop Stop so we can find the problem.
End Select
Similarly, use an Else statement in a long series of If Then Else statements.
If Filter = filter_PREWITT Then
:
ElseIf Filter = filter_HIGH_PASS Then
:
ElseIf Filter = filter_LOW_PASS Then
:
Else This should not happen.
Stop Stop so we can find the problem.
End If
Define Enumerated ValuesAdding or removing a value from an enumerated type can change the meanings of the remaining values. Visual Basic assigns numeric values to enumerated types in the order in which they are defined. By default, Visual Basic gives the first the value 0. It gives subsequent symbols a value 1 greater than the previous value. In the following code, the comments indicate the values assigned by Visual Basic. Public Enum btBorderType btNone 0 btFancy 1 btSingle 2 btDouble 3 End Enum If you later decide to remove the value btFancy, Visual Basic will renumber the other values so btSingle is 1 and btDouble is 2. If your program uses only these symbolic values, this does not matter. If you save the values in a file or database, however, the new numbering will not agree with the saved data. If you save enumerated values to a file or database, explicitly assign numeric values to them. Then you can add, remove, or rearrange them without changing their values. Public Enum btBorderType btNone = 0 btFancy = 1 btSingle = 2 btDouble = 3 End Enum Group Related ValuesPlace related constants and enumerated values in the same module so they are easy to find. Also put them next to each other within the module. This does not mean you have to create a separate module for every set of constants. In fact, some developers put all constants in the same module whether or not they are related. Whether you define constants in one module or many, keep related values together. Self-TestProgram Bad4 violates some of the guidelines described in this chapter. The program is shown in Figure 4.1 randomizing a list of user-entered values. Fill in the TextBoxes on the left and click the Randomize button. The program rearranges the items randomly and displays them in the labels on the right. Appendix A, Self-Test Solutions, contains one possible improved version of the following Bad4 code. Remember that this is not the only possible solution. You can download updated bad and good versions of this program from the Web at www.vb-helper.com/err.htm.
Option Explicit
Private Values(0 To 9) As String
Randomize the values entered by the user.
Private Sub cmdRandomize_Click()
LoadValues Values, 0, 9
RandomizeValues Values, 0, 9
DisplayValues Values, 0, 9
End Sub
Load the values from the txtInput TextBoxes.
Private Sub LoadValues(list() As String, _
min As Integer, max As Integer)
Dim i As Integer
For i = min To max
list(i) = txtInput(i).Text
Next i
End Sub
Randomize the values in the array.
Private Sub RandomizeValues(list() As String, _
min As Integer, max As Integer)
Dim i As Integer
Dim new_index As Integer
Dim temp_string As String
Randomize
For i = min To max - 1
Randomly pick an entry with index between
i and max to go in position i.
new_index = Int((max - i + 1) * Rnd + i)
Swap that entry with the one in position i.
temp_string = Values(i)
Values(i) = Values(new_index)
Values(new_index) = temp_string
Next i
End Sub
Display the values in the lblOutput Labels.
Private Sub DisplayValues(list() As String, _
min As Integer, max As Integer)
Dim i As Integer
For i = min To max
lblOutput(i).Caption = list(i)
Next i
End Sub
SummaryConstants and enumerated types can make code easier to read, understand, and modify. Use them whenever possible. Many experienced developers believe that code should contain no hard-coded numbers except 0 and 1, and then only when they are used as lower array bounds. Other developers believe even lower bounds should be defined as constants. The following Bug Stoppers restate the main concepts presented in this chapter.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|